package com.lgkj.ui;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.NewAdapter;
import com.lgkj.bean.NewsBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/9 0009.
 */

public class NewListActivity extends BaseActivity {
    @BindView(R.id.lv_movie)
    ListView lvMovie;
    private NewAdapter adapter;
    private NewsBean newsBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_news;
    }

    @Override
    public void initView() {
        setTitle("新闻列表");
        adapter = new NewAdapter(NewListActivity.this);
        lvMovie.setAdapter(adapter);
        lvMovie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                controlSb(adapter.getItem(i).getUrl());
//                Intent intent = new Intent(NewListActivity.this, WebViewActivity.class);
//                intent.putExtra("url", adapter.getItem(i).getUrl());
//                intent.putExtra("title", "新闻详情");
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("news_type", "1");
        HttpUtils.postHttp(map, CommonConstant.newslist, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("status") == 1) {
                    newsBean = new Gson().fromJson(String.valueOf(jsonObject), NewsBean.class);
                    if (newsBean != null && newsBean.getData() != null) {
                        adapter.setData(newsBean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }


    private void controlSb(String url) {
        showDiaLog("正在打开");
        String ip = SharedPreferencesUtils.getParam(NewListActivity.this, "ip", "").toString();
        final String phone = SharedPreferencesUtils.getParam(NewListActivity.this, "phone", "").toString();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("contral", "");
        map.put("id", url);
        map.put("open", "item3");
        HttpUtils.getHttp(map, ip + CommonConstant.control, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                toastLong("当前不是一个wifi，请选链接一个wifi", 0);
            }
        });
    }
}
